


<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>设置我的密码</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../layui/css/admin.css" media="all">
</head>
<body>

  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-header">修改密码</div>
          <div class="layui-card-body" pad15>
            
            <div class="layui-form" lay-filter="">
              <div class="layui-form-item">
                <label class="layui-form-label">当前密码</label>
                <div class="layui-input-inline">
                  <input type="password" name="oldPassword" lay-verify="required" lay-verType="tips" class="layui-input">
                </div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-inline">
                  <input type="password" name="pwd1" lay-verify="pass" lay-verType="tips" autocomplete="off" id="LAY_password" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">6到16个字符</div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label">确认新密码</label>
                <div class="layui-input-inline">
                  <input type="password" name="pwd2" lay-verify="repass" lay-verType="tips" autocomplete="off" class="layui-input">
                </div>
              </div>
              <div class="layui-form-item">
                <div class="layui-input-block">
                  <button class="layui-btn" lay-submit lay-filter="setmypass">确认修改</button>
                </div>
              </div>
            </div>
            
          </div>
        </div>
      </div>
    </div>
  </div>
<script src="../JS/utils/request.js"></script>
  <script src="../JS/utils/auth.js"></script>
  <script src="../layui/layui.all.js"></script>  
  <script>
  layui.use(['form', 'jquery','layer'],function(){
            let form = layui.form;
            let $ = layui.$;
            let layer = layui.layer;

            $('button[lay-filter="setmypass"]').click(function(){
              // 判断两次密码输入是否一致
            const pwd1 = $('input[name="pwd1"]').val()
            const pwd2 = $('input[name="pwd2"]').val()
            if (pwd1 !== pwd2) {
              layer.msg('两次输入的密码不一致');
              return false;
            }else{
              let userId = localStorage.getItem('userId')
              $.ajax({
              url: '/users/getPassword', // 替换为实际接口地址
              type: 'GET',
              headers:{
              'Authorization':localStorage.getItem('token')
              },
              data:{
                 password:$('input[name="oldPassword"]').val(),
                 userId:userId
              },
               success: function(res) {
                 console.log(res)
                // 填充表单数据
              if(res.data === 1){
                     $.ajax({
            url: '/users/update',
            method: 'PUT',
            contentType: 'application/json',
            data: JSON.stringify({password:$('input[name="pwd1"]').val(),userId:userId}),
            headers:{
              'Authorization':localStorage.getItem('token')
              },
            success: function(res){
              console.log(res)
              if(res.code === 0){
                layer.msg('密码重置成功，即将退出重新登录！')
                        // 清除本地存储
                localStorage.removeItem('token');
                localStorage.removeItem('username');
                localStorage.removeItem('avatar');
                localStorage.removeItem('userId');
                setTimeout(function(){
                parent.location.reload(); // 跳转首页
          }, 1500);
              } else {
                layer.msg(res.msg || '更新失败');
                
              }
            },
            error: function(xhr){
              layer.msg('请求失败: 院系不能为空！' + xhr.statusText);
              
            }
          });
              }else{
                layer.msg('当前密码输入有误！');
              }
              },
              error: function(xhr, status, error) {
                console.error('分类数据获取失败:', error);
              }
              })
            }
            })
  });
  </script>
</body>
</html>